<template>
  <div id="single">
    <van-nav-bar @click-left="back" left-arrow left-text="返回" title="聊天记录" />
    <div class="content">
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
        <div v-for="(item,index) in list" :key="item.id" class="contentIN">
          <div class="leftd" v-if="item.isuser === 0">
            <div class="timer" style="text-align: center">{{item.created}}</div>
            <div class="cont contLeft">
              <img @click="toDetail(index)" :src="item.img == '' || item.img == null ? require('@/assets/images/avatar.png') : item.img" alt="" />
              <p class="name">{{item.name}}</p>
              <div v-if="item.type == 0" class="speech left" ng-class="speech left">{{ item.info }}</div>
              <div v-if="item.type == 1" class="speech left" ng-class="speech left"  @click="Preview(list,index)">
                <img class="inforImg" :src='apiConfig.imgUrl + item.info' alt="" />
              </div>
              
             
              <!-- <div class="biaojiLeft" v-if="item.isread==1" style="color: #404040">未读</div>
              <div class="biaojiLeft" v-else>已读</div> -->
            </div>
          </div>
          <div class="rightd" v-if="item.isuser === 1">
            <div class="timer" style="text-align: center">{{item.created}}</div>
            <div class="cont contRight">
              <img :src="item.img == '' || item.img == null ? require('@/assets/images/avatar.png') : item.img" alt=""  />
              <div>
                <p class="name">{{item.name}}</p>
                <div v-if="item.type == 0" class="speech right" ng-class="speech right">{{ item.info }}</div>
                <div v-if="item.type == 1" class="speech right" ng-class="speech right" @click="Preview(list,index)">
                  <img class="inforImg" :src='apiConfig.imgUrl + item.info' alt="" />
                </div>
              </div>
              <!-- <div class="biaojiRight" v-if="item.isread==1" style="color: #404040">未读</div>
              <div class="biaojiRight" v-else style="color:#6b6b6b;">已读</div> -->
            </div>
          </div>
        </div>
      </van-pull-refresh>
    </div>
    <div class="footer">
      <van-form @submit="onSubmit">
        <van-row type="flex" justify="space-between" align="center">
          <!-- <van-col span="2"><img src="@/assets/images/b90855f78c23983638ec1d49c60f658.png" alt=""></van-col> -->
         <van-col span="17">
            <van-field
              class="input"
              v-model="form.info"
              rows="1"
              autosize
              type="textarea"
              placeholder="请输入留言"
            />
          </van-col>
          <van-col class="footBtns" span="7">
            <div class="sendImg">
               <van-uploader
                  :after-read="afterRead"
                  v-model="filelist"
                >
                <img src="@/assets/images/add.png" alt="" />
              </van-uploader>
            </div>
            <van-button class="submit" round type="info" size="small">发送</van-button>
          </van-col>
        </van-row>
      </van-form>
    </div>
  </div>
</template>

<script>
import {messagedetail,addmessage,send_img} from "@/apis/patient";
import apiConfig from "../../../config/api.config"
import { uploadImgs } from "../../../config/upload"
import { ImagePreview } from 'vant';
export default {
  name: "single.vue",
   components: {
      ImagePreview
    },
  data() {
    return {
      apiConfig,
      uploadImgs,
      fileList:[],
      list: [],
      isLoading: false,
      finished: false,
      count:0,
      refreshing: false,
      users:[],
      txImg:'',
      form:{
        fsty:2,
        info: "",
      },
      info:{
        hzid:'',
        ty:2,
        page:1
      }
    }
  },
  methods: {
    
    back() {
      this.$router.go(-1); //返回上一层
    },
    messageInfo(){
      messagedetail(this.info)
      .then(res=>{
        if(res.codes == "200"){
          this.list = res.data.list
          this.info.page ++
        }
      }).catch(err=>{
         if(err.msg){
          if(err.codes=="400"){
            this.$toast(err.msg)
          }else{
            this.$toast(err.msg);
            }
        }else{
          this.$toast("网络错误"); 
        }
      })
    },
    onSubmit() {
      let that = this
     if(that.form.info !== ""){
        addmessage(this.form)
          .then(res=>{
          // this.$toast('发送成功')
          this.form.info = ''
          this.info.page = 1
          this.messageInfo()
          that.$nextTick(() => {
            var container = that.$el.querySelector(".content  .van-pull-refresh");
            console.log(container.scrollHeight)
            container.scrollTop = container.scrollHeight;
          })
          // console.log(this.list)
          this.scrollToBottom()
        }).catch(err=>{
          if(err.msg){
            this.$toast(err.msg);
          }else{
            this.$toast("网络错误"); 
          }
        })
      }else{
        this.$toast("不能发送空白信息")
      }
    },
    toDetail(index) {
      this.list.forEach((item,idx) =>{
        if(index == idx){
          if(item.userty == "患者"){
            this.$router.push(`/patientDetail/${this.info.hzid}`)
          }
        }
      })
    },
    onRefresh() {
      messagedetail(this.info).then(res => {
        if(res.codes == "200"){
          this.info.page++
          res.data.list.forEach(item=>{
            this.list.unshift(item);
          })
        }
        this.isLoading = false;
      }).catch(err => {
        if(err.msg){
          if (err.codes === 400) {
            this.isLoading = false;
            this.$toast(err.msg)
          }else{
            this.$toast(err.msg);
          }
        }else{
          this.$toast("网络错误"); 
        }
        
      })
    },
    scrollToBottom() {
     var that = this
      setTimeout(function(){ 
        that.$nextTick(() => {
          var container = that.$el.querySelector(".content  .van-pull-refresh");
          console.log(container.scrollHeight)
          container.scrollTop = container.scrollHeight;
        })
      },1000) 
    },
  // 图片发送
   afterRead(file) {
     console.log(file)
      this.uploadImgs(file)
      setTimeout(()=>{
        if( sessionStorage.getItem('base64') != '' && sessionStorage.getItem('base64')!=null){
          this.form.file = sessionStorage.getItem('base64');
          send_img(this.form)
          .then(res=>{
              if(res.codes == 200){
                this.form.info = ''
                this.info.page = 1
                this.messageInfo()
                this.scrollToBottom()
              }
          })
          .catch(err=>{
            if(!err.codes){
              this.$toast("网络错误");
            }else{
              this.$toast(err.msg);
            }
          })
        
        sessionStorage.removeItem('base64')
      }else{
        this.$toast("发送失败");
      }
      },1600)
    
    },
    Preview(imgs,index) {
      let arr = [];
      let startImg  = ''
      // 开始图片索引
      let startIndex  = ''
      for (let i = 0; i < imgs.length; i++) {
        startImg = imgs[index].info
        if(imgs[i].type == 1){
            const element = imgs[i].info;
            arr.push(apiConfig.imgUrl+element)
        }else{
          
        }
      }
      arr.forEach((item,idx) => {
        if(item == apiConfig.imgUrl+ startImg){
          startIndex = idx
        }
      })
      ImagePreview({images:arr,startPosition:startIndex})
    },
  
  },
  mounted() {
    this.messageInfo()
    this.scrollToBottom()
    
  },

  created() {
    this.form.hzid = Number(this.$route.query.id);
    this.form.ysid = Number(this.$route.query.ysid);
    this.form.gwid = Number(this.$route.query.gwid);
    this.info.hzid = Number(this.$route.query.id);
    this.info.ysid = Number(this.$route.query.ysid);
    this.info.gwid = Number(this.$route.query.gwid);
    this.txImg = localStorage.getItem('tx')
    // localStorage.removeItem('tx')

  }
}
</script>

<style lang="less">
#single {
  text-align: left;
  .van-nav-bar {
    .van-nav-bar__text,
    .van-icon {
      color: #404040;
    }
  }
  .footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 10px 10px 20px 10px;
    background: #efefef;
    .input {
      background: #fff;
      padding: 3px 5px;
      border-radius: 3px;
    }
    .van-button--round {
      border-radius: 999px;
      width: 100%;
    }
    // .van-cell {
    //   padding: 2px;
    // }
  }
  .van-nav-bar {
    position: fixed;
    top: 0;
    width: 100%;
  }
  .content {
    position: relative;
    top: 50px;
    height: 90vh;
    padding-bottom: 80px;
    overflow-y: scroll;
    &::-webkit-scrollbar {
      display: none;
    }
    .van-pull-refresh {
      height: 100%;
      overflow: scroll;
      border: none;
      &::-webkit-scrollbar {
        display: none;
      }
    }
  }
  // 图片上传按钮
 .footBtns{
      display: flex;
      justify-content: space-around;
      .sendImg{
        width: 30px;
        height: 30px;
        .van-uploader__input-wrapper{
          img{
            width: 30px;
            height: 30px;
          }
          input{
            height: 30px;
          }
        }
      }
      .van-uploader__preview{
        display: none;
      }
      .submit{
        width: 60px;
      }
    }
  /* 微信气泡 */

  div.speech {

    float: left;
    margin: 20px 0;
    padding: 8px;
    table-layout: fixed;
    word-break: break-all;
    position: relative;
    border-radius: 8px;
  }

  div.speech.right {
    display: inline-block;
    
    box-shadow: -2px 2px 5px #ccc;
    margin-right: 10px;
    max-width: 244px;
    float: right;
    font-size: 17px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #ffffff;
    line-height: 22px;
    background: linear-gradient(360deg, #4a8bd6 0%, #729af3 100%);
    border-bottom-right-radius: 0;
  }

  div.speech.right:before {
    content: "";
    position: absolute;
    width: 0;
    height: 0;
    top: 9px;
    bottom: auto;
    left: auto;
    right: -10px;
    border-width: 9px 0 9px 10px;
    border-color: transparent #989898;
  }

  div.speech.right:after {
    content: "";
    position: absolute;
    width: 0;
    height: 0;
    top: 10px;
    bottom: auto;
    left: auto;
    right: -8px;
    border-width: 8px 0 8px 9px;
    border-color: transparent #bced50;
  }

  div .left {
    display: inline-block;
    box-shadow: 2px 2px 2px #cccccc;
    margin-left: 10px;
    max-width: 244px;
    position: relative;
    background: #efefef;
    font-size: 17px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #262628;
    line-height: 22px;
    border-bottom-left-radius: 0;
    
  }
  .cont {
    position: relative;
  }
  .contentIN{
    padding: 0 10px;
  }
  .contentIN,
  .cont {
    overflow: hidden;
    *zoom: 1;
  }
  .leftd {
    clear: both;
    float: left;
    width: 100%;
    .timer {
      margin: auto;
      width: 191px;
      font-size: 14px;
      color: #9b9b9b;
    }
    .inforImg{
      width: 160px;
      height: auto;
      border-radius: 0;
    }
    img{
      display: block;
      width: 40px;
      height: 40px;
      border-radius: 50%;
      // margin: 20px 0;
    }
    .name{
      position: absolute;
      top: 0;
      left: 50px;
      font-size: 10px;
    }
  }

  .rightd {
    clear: both;
    float: right;
    width: 100%;
    .timer {
      margin: auto;
      width: 191px;
      font-size: 14px;
      color: #9b9b9b;
    }
    .inforImg{
      width: 160px;
      height: auto;
      border-radius: 0;
    }
    img{
      display: block;
      width: 40px;
      height: 40px;
      border-radius: 50%;
      // margin: 0.26667rem 0;
    }
    .name{
      position: absolute;
      top: 0;
      right: 50px;
      font-size: 10px;
    }
  }

  .leftd_h {
    width: 39px;
    height: 39px;
    border-radius: 100%;
    display: block;
    float: left;
    overflow: hidden;
  }

  .leftd_h img {
    display: block;
    width: 100%;
    height: auto;
  }

  .rightd_h {
    width: 39px;
    height: 39px;
    border-radius: 100%;
    display: block;
    float: right;
    overflow: hidden;
  }

  .rightd_h img {
    display: block;
    width: 100%;
    height: auto;
  }
  .contLeft {
    display: flex;
    // align-items: flex-end;
    .biaojiLeft {
      margin: 0 0 10px 10px;
    }
  }
  .contRight {
    display: flex;
    flex-direction: row-reverse;
    // align-items: flex-end;
    .biaojiRight {
      margin: 0 10px 10px 0;
    }
  }
  ::-webkit-scrollbar-track {
    box-shadow: inset 0 0 0.13333rem rgba(0, 0, 0, 0);
    background: none;
    border-radius: 0.13333rem;
  }
}
</style>
